#include "../include/Thread.hpp"

#include <stdio.h>
#include <string.h>

namespace cdm{

Thread::Thread(ThreadCallback &&cb)
:_thdId(0),
_isRunning(false),
_cb(cb){

}

Thread::~Thread(){

}

void Thread::start(){
    if(!_isRunning){
        int ret = pthread_create(&_thdId,NULL,start_routine,this);
        if(ret != 0){
            fprintf(stderr,"%s",strerror(ret));
            return;
        }
        _isRunning = true;
    }
}

void Thread::join(){
    if(_isRunning){
        pthread_join(_thdId,NULL);
        _isRunning = false;
    }
}

void*  Thread::start_routine(void *arg){
    Thread* threadptr = static_cast<Thread*>(arg);
    if(threadptr){
        threadptr->_cb();
    }
    return NULL;
}

}//end of namespace

